<template>
  <div>
    <el-card>
      <div style="padding: 0 0 20px 0;">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>用户管理</el-breadcrumb-item>
          <el-breadcrumb-item>项目</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-form ref="form" :model="form" label-width="80px" :inline="true">
        <!--<el-form-item>
          <el-input
            v-model="form.title"
            clearable
            placeholder="请输入关键字"
            class="input-with-select"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch()">搜索</el-button>
        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="add">添加项目</el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="table"
        :data="dataList"
        border
        lazy
        :load="load"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="项目名称" prop="username"></el-table-column>
        <el-table-column label="项目公司" prop="company_name"></el-table-column>
        <el-table-column label="代理商" prop="nick_name"></el-table-column>
        <el-table-column
          label="项目管理员"
          prop="manager_name"
        ></el-table-column>
        <el-table-column
          label="项目地址"
          prop="project_address"
        ></el-table-column>
        <el-table-column
          label="维护单位"
          prop="maintain_company"
        ></el-table-column>
        <el-table-column label="操作" width="350px">
          <template slot-scope="scope">
            <el-button type="primary" @click="addSubProject(scope.row)"
              >新增子项目</el-button
            >
            <el-button type="primary" @click="edit(scope.row)">修改</el-button>
            <el-button type="danger" @click="del(scope.row)">删除</el-button>
            <el-button type="primary" @click="deviceList(scope.row)">设备列表</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :total="total"
        :current-page="query.page"
        :page-size="10"
        @current-change="changePage"
      ></el-pagination>
      <div style="height: 20px;"></div>
    </el-card>

    <el-dialog :visible.sync="showAddSubProject" title="新增子项目">
      <addsubproject
        :data="curProjectData"
        :visible.sync="showAddSubProject"
        @success="addSubSuccess"
      />
    </el-dialog>

    <el-dialog :visible.sync="showAddProject" title="添加项目">
      <addproject :visible.sync="showAddProject" @success="getData()" />
    </el-dialog>

    <el-dialog :visible.sync="showEditProject" title="编辑项目">
      <editproject
        :visible.sync="showEditProject"
        :data="editData"
        @success="getData()"
      />
    </el-dialog>

    <el-dialog :visible.sync="showSubEditProject" title="编辑项目">
      <editsubproject
        :visible.sync="showSubEditProject"
        :data="editData"
        @success="getData()"
      />
    </el-dialog>

    <el-dialog :visible.sync="showDeviceList" title="设备列表">
      设备列表 
    </el-dialog>
  </div>
</template>

<script>
import { page, getsubproject, delproject } from '@/api/user/project.js';
import addsubproject from './components/addsubproject.vue';
import addproject from './components/addproject.vue';
import editproject from './components/editproject.vue';
import editsubproject from './components/editsubproject.vue';

export default {
  components: {
    addsubproject,
    editproject,
    addproject,
    editsubproject,
  },
  data() {
    return {
      maps: new Map(),
      form: {},
      showAddSubProject: false,
      showAddProject: false,
      showEditProject: false,
      showSubEditProject: false,
      showDeviceList:false,
      query: {
        page: 1,
      },
      total: 0,
      dataList: [],
      curProjectData: {},
      editData: {},
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      page(this.query).then((res) => {
        this.dataList = res.data.list.data;
        this.dataList = this.dataList.map((item) => {
          item.hasChildren = true;
          return item;
        });
        this.total = res.data.list.total;
      });
    },
    changePage(e) {
      console.log(e, 'eeee');
    },
    addSubProject(e) {
      this.showAddSubProject = true;
      this.curProjectData = e;
    },
    deviceList(e){
      this.showDeviceList = true;
      this.curProjectData = e;
    },
    addSubSuccess() {
      const { tree, treeNode, resolve } = this.maps.get(this.curProjectData.id);
      this.$set(this.$refs.table.store.states.lazyTreeNodeMap, this.curProjectData.id, []);
      this.load(tree, treeNode, resolve);
    },
    add() {
      this.showAddProject = true;
    },
    edit(row) {
      this.editData = row;
      console.log(row, '看看是什么类型');
      if (row.pid > 0) {
        this.showSubEditProject = true;
      } else {
        this.showEditProject = true;
      }
    },
    onSearch() {},
    load(tree, treeNode, resolve) {
      this.maps.set(tree.id, { tree, treeNode, resolve });
      getsubproject(tree.id).then((res) => {
        let data = res.data.map((item) => {
          item.hasChildren = true;
          return item;
        });
        resolve(data);
      });
    },
    del(row) {
      this.$confirm('你确定要删除项目ID为' + row.id + '的数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delproject(row.id).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
            if (row.pid == 0) {
              this.dataList = [];
              this.getData();
            } else {
              const { tree, treeNode, resolve } = this.maps.get(row.pid);
              this.$set(
                this.$refs.table.store.states.lazyTreeNodeMap,
                row.pid,
                []
              );
              this.load(tree, treeNode, resolve);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
  },
};
</script>

<style scoped></style>
